import { GameConfig } from "../configs/GameConfig"
import { AdsUtil } from "../etc/AdsUtil"
import Appearance from "../model/Appearance"
import { ClothAdWindow } from "../ui/ClothAdWindow"
import { PlayerModuleC, PlayerModuleS } from "./PlayerModule"

export enum EnumAppearanceType {
    cloth = 'cloth',
    handEffect = 'handEffect'
}

export class ClothModuleData extends Subdata {
    //旧数据, 不要使用
    @Decorator.persistence()
    characterAppearanceAssetGUID: string

    @Decorator.persistence()
    appearanceCfgMapping: { [type: number]: number }
}

export class ClothModuleC extends ModuleC<ClothModuleS, ClothModuleData> {

    onStart() {
        for (const cfg of GameConfig.Cloth.getAllElement()) {
            let displayObj = GameObject.findGameObjectById(cfg.DisplayObjectID)
            let trigger = <Trigger>displayObj.getChildByName('触发器')
            trigger.onEnter.add(this.onClothTriggerEnter.bind(this, cfg.ID))
            if(cfg.Type == EnumAppearanceType.cloth){
                let displayCharacter = <Character>displayObj.getChildByName('外观')
                displayCharacter.asyncReady().then(character => {
                    character.setDescription([cfg.Data[0]])
                    character.displayName = ' '
                    let anim = character.loadAnimation('29755')
                    anim.loop = 0
                    anim.play()
                })
            }
            else if(cfg.Type == EnumAppearanceType.handEffect){
                let anchor = displayObj.getChildByName('旋转点')
                let sf = Number(cfg.Data[1])
                let scale = new Vector(sf,sf,sf)
                EffectService.playOnGameObject(cfg.Data[0], anchor, {
                    position: new Vector(0, 0, 150),
                    loopCount: 0,
                    scale: scale
                })
                EffectService.playOnGameObject(cfg.Data[0], anchor, {
                    position: new Vector(0, 0, -150),
                    loopCount: 0,
                    scale: scale
                })
            }
        }
    }

    onClothTriggerEnter(cfgID: number, other: GameObject) {
        if (!(other instanceof Character)) return;
        if (<Character>other != Player.localPlayer.character) return;

        let window = UIService.show(ClothAdWindow)
        window.setInfo(() => {
            AdsUtil.playRewardAds(
                () => {
                    this.server.net_unlockCloth(cfgID)
                },
                () => {
                    UIService.hideUI(window)
                },
            )
        }, this)
    }
}

export class ClothModuleS extends ModuleS<ClothModuleC, ClothModuleData> {
    private playerMD:PlayerModuleS

    onStart() {
        this.playerMD = ModuleService.getModule(PlayerModuleS)
        // 玩家外观脚本初始化完成
        this.playerMD.onPlayerNamePullSuccess.add(this.refreshPlayerAppearance, this)
    }

    net_unlockCloth(cfgID: number) {
        let cfg = GameConfig.Cloth.getElement(cfgID)
        if(!this.currentData.appearanceCfgMapping){
            this.currentData.appearanceCfgMapping = {}
        }
        this.currentData.appearanceCfgMapping[cfg.Type] = cfg.ID
        this.currentData.save(true)
        this.refreshPlayerAppearance(this.currentPlayerId)
    }

    private refreshPlayerAppearance(playerID:number){
        let appearance = Appearance.AppearanceMap.get(playerID)
        if(!appearance) return

        let data = this.getPlayerData(playerID)
        if(!data.appearanceCfgMapping) return

        if(data.appearanceCfgMapping[EnumAppearanceType.cloth]){
            appearance.clothCfgID = data.appearanceCfgMapping[EnumAppearanceType.cloth]
        }
        if(data.appearanceCfgMapping[EnumAppearanceType.handEffect]){
            appearance.handEffectCfgID = data.appearanceCfgMapping[EnumAppearanceType.handEffect]
        }
    }
}